import util from '../../utils/util'
import {
  memberPayModel,
  wxPayModel,
} from '../../models/payment.js'
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    orderId: {
      type: Number,
      bserver(value) {
        console.log(value)
      }
    },
    orderList: {
      type: Object,
    },
    height: {
      type: String,
      observer(value) {
        this._heightAnimation(value)
        console.log(value)
      }
    },
    isShade: {
      type: Boolean,
      value: true
    }

  },

  /**
   * 组件的初始数据
   */
  data: {
    detail: true,
    isPassWord: true,
    animationHeight: {},
    animationLeft: {},
    animationLefts: {},
    animationHeightPass: {},
    isPayment: true,
    isPaymentNum: 0,
    isWxPay: true,
    payText: "微信支付",
    passWord: [],
    numList: []
  },

  /**
   * 组件的方法列表
   */
  //组件生命周期函数，在组件实例进入页面节点树时执行
  attached() {
    this._passWord()
    console.log(this.properties.height)
    this._heightAnimation(this.properties.height)
  },
  methods: {
    //点击立即支付
    payment() {
      if (this.data.isPaymentNum) {
        this._heightAnimation(0)
        this._heightAnimationPass('800rpx')
      } else {
        this.wxPay()
      }
    },
    //微信支付
    wxPay() {
      if (this.data.isPayment) {
        //点击一次后，进行支付接口调用，禁止再次点击
        this._isPaymentBoolean(false)
        //支付
        const id = this.properties.orderId
        wxPayModel(id).then((item) => {
          const res = item.data
          const $this = this
          wx.requestPayment({
            'timeStamp': `${res.timeStamp}`,
            'nonceStr': `${res.nonceStr}`,
            'package': `${res.package}`,
            'signType': `${res.signType}`,
            'paySign': `${res.paySign}`,
            'success'() {
              util.showToast('支付成功', 2000, 'sucsess')
              console.log('微信支付成功')
            },
            'fail'() {
              $this._isPaymentBoolean(true)
              console.log('微信支付失败')
            },
          })
        })
      }
    },
    //会员卡支付
    memberPay() {
      const passwd = this.data.numList.join('')
      const id = this.properties.orderId
      console.log(id)
      memberPayModel(id, passwd).then((res) => {
        if (res.errorCode == 10000) {
          this._passwordMistakes()
          console.log('密码错误')
        } else if (res.errorCode == 0) {
          util.showToast('支付成功！', 2000, 'success')
          console.log('会员卡支付成功')
        } else {
          util.showToast('数据异常')
          console.log(res.msg)
        }
        //清空
        this.setData({
          numList: []
        })
        this.triggerEvent('close')
      })
    },
    //点击键盘上的数字
    keyPass(e) {
      const num = e.target.dataset.num
      let numList = this.data.numList
      if (numList.length < 6) {
        numList.push(num)
        if (numList.length <= 5) {
          this.setData({
            numList
          })
        }
        if (numList.length == 6) {
          this.setData({
            numList,
            isShade: true
          })
          this._heightAnimationPass(0)
          this.memberPay()
        }
      }
    },
    // 关闭详情
    closeDetail() {
      this.setData({
        isPayment: true,
      })
      this.triggerEvent('close')
    },
    //关闭键盘
    colsePassword() {
      this.setData({
        isShade: true,
        numList: []
      })
      this._heightAnimationPass(0)
      this.triggerEvent('close')
    },
    // 点击键盘上的删除
    keyDel() {
      const numList = this.data.numList
      numList.pop()
      console.log(numList)
      this.setData({
        numList
      })
    },
    //选择支付方式
    payPaySelect() {
      this._leftAnmation('-100%')
    },
    //选择微信支付
    payWx() {
      this.setData({
        isWxPay: true,
        payText: '微信支付',
        isPaymentNum: 0
      })
      this.goReturn()
    },
    // 选择会员卡支付
    payMember() {
      this.setData({
        isWxPay: false,
        payText: '会员卡支付',
        isPaymentNum: 1
      })
      this.goReturn()
    },
    //返回支付
    goReturn() {
      this._leftAnmation(0)
    },
    //密码输入错误
    _passwordMistakes() {
      const $this = this
      wx.showModal({
        content: "密码输入错误",
        confirmText: '重试',
        success(res) {
          if (res.confirm) {
            $this.setData({
              isShade: false
            })
            $this._heightAnimationPass('800rpx')
          }
        }
      })
    },
    //提交表单高度动画
    _heightAnimation(height) {
      const animation = wx.createAnimation({
        duration: 200,
      })
      animation.height(height).step()
      this.setData({
        animationHeight: animation.export(),
      })
    },
    //会员卡密码高度动画
    _heightAnimationPass(height) {
      const animation = wx.createAnimation({
        duration: 200,
      })
      animation.height(height).step()
      this.setData({
        animationHeightPass: animation.export(),
      })
    },
    // left动画
    _leftAnmation(left) {
      const animation = wx.createAnimation({
        duration: 200,
      })
      animation.left(left).step()
      this.setData({
        animationLeft: animation.export(),
      })
    },
    //是否允许用户点击
    _isPaymentBoolean(isState) {
      this.setData({
        isPayment: isState
      })
    },
    //会员卡键盘
    _passWord() {
      const passWord = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0]
      this.setData({
        passWord
      })
    },
  }
})